package com.kun.alipay.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 支付宝EasySDK配置类
 */
@Slf4j
@Component
public class AlipayEasySdkConfig implements ApplicationRunner {

    @Resource
    private AlipayConfig alipayConfig;

    @Override
    public void run(ApplicationArguments args) {
        Factory.setOptions(getOptions());
        System.out.println("**********支付宝SDK初始化完成**********");
    }

    private Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = alipayConfig.getURL();
        config.signType = alipayConfig.getSIGNTYPE();
        config.appId = alipayConfig.getAPPID();
        config.merchantPrivateKey = alipayConfig.getRSA_PRIVATE_KEY();
        config.alipayPublicKey = alipayConfig.getALIPAY_PUBLIC_KEY();
        config.notifyUrl = alipayConfig.getNotify_url();
        return config;
    }

}